package com.john.blog.controller.front;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.john.blog.common.constant.KeyConst;
import com.john.blog.common.vo.CommonResult;
import com.john.blog.controller.front.support.FrontBlogControllerSupport;
import com.john.blog.model.po.Blog;
import com.john.blog.model.po.Category;
import com.john.blog.model.po.Model;
import com.john.blog.model.po.Tags;
import com.john.blog.service.BlogService;
import com.john.blog.service.CategoryService;
import com.john.blog.service.TagsService;

import tk.mybatis.mapper.entity.Example;

/**
 * 博客
 * 
 * @author JiangGengchao
 * @date 2018年2月5日
 */
@RestController
public class FrontBlogController extends FrontBlogControllerSupport {

    @Autowired
    private BlogService blogService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private TagsService tagsService;

    /**
     * 轮播图巨幕
     * 
     * @return
     */
    @RequestMapping("/carousel")
    public CommonResult carousel() {
        Example example = new Example(Blog.class);
        example.createCriteria().andIsNotNull("picture").andEqualTo("status", 0);
        example.orderBy("createDate").desc();
        PageInfo<Blog> blogPageInfo = blogService.findPagination(example, 1, 6);
        return CommonResult.build(CommonResult.Code.SUCCESS, null, blogPageInfo.getList());
    }

    /**
     * 热门排行
     */
    @RequestMapping("/hotRank")
    public CommonResult hotRank() {
        Example example = new Example(Blog.class);
        example.orderBy("readNum").desc();
        example.orderBy("commentNum").desc();
        PageInfo<Blog> recommendPageInfo = blogService.findPagination(example, 1, 5);
        return CommonResult.build(CommonResult.Code.SUCCESS, null, recommendPageInfo.getList());
    }

    /**
     * 分页显示blog
     * 
     * @param request
     * @return
     */
    @RequestMapping("/showPaging")
    public CommonResult showPaging(@RequestParam(value = KeyConst.PAGE_NUM) Integer pageNum, @RequestParam(value = KeyConst.PAGE_SIZE) Integer pageSize) {
        // 查询数据
        PageInfo<Blog> blogPageInfo = blogService.findPagination(null, pageNum, pageSize);
        return CommonResult.build(CommonResult.Code.SUCCESS, null, blogPageInfo);
    }

    // ======================右侧分页==========================
    /**
     * 文章分类
     */
    @RequestMapping("/category")
    public CommonResult category() {
        Example example = new Example(Category.class);
        example.orderBy("blogNum").desc();
        List<Category> categoryList = categoryService.selectByExample(example);
        return CommonResult.build(CommonResult.Code.SUCCESS, null, categoryList);
    }

    /**
     * 最新blog
     * 
     * @param request
     * @return
     */
    @RequestMapping("/show")
    public CommonResult show(@RequestParam(value = KeyConst.PAGE_NUM) Integer pageNum, @RequestParam(value = KeyConst.PAGE_SIZE) Integer pageSize) {
        // 查询数据
        PageInfo<Blog> blogPageInfo = blogService.findPagination(null, pageNum, pageSize);
        return CommonResult.build(CommonResult.Code.SUCCESS, null, blogPageInfo.getList());
    }

    /**
     * 标签 云
     */
    @RequestMapping("/blogTags")
    public CommonResult blogTags() {
        List<Tags> data = tagsService.findAll();
        return CommonResult.build(CommonResult.Code.SUCCESS, null, data);
    }

    /**
     * 猜你喜欢 推荐
     */
    @RequestMapping("/recommend")
    public CommonResult recommend(@RequestParam(value = KeyConst.PAGE_NUM) Integer pageNum, @RequestParam(value = KeyConst.PAGE_SIZE) Integer pageSize) {
        Example example = new Example(Blog.class);
        example.orderBy("readNum").desc();
        example.orderBy("commentNum").desc();
        PageInfo<Blog> recommendPageInfo = blogService.findPagination(example, pageNum, pageSize);
        return CommonResult.build(CommonResult.Code.SUCCESS, null, recommendPageInfo.getList());
    }

    // ===========================================
    /**
     * 增加阅读数, 同时返回博文现有阅读数
     */
    @RequestMapping("/openRead")
    public CommonResult openRead(@RequestParam(value = Model.ID) String id) {
        if (id == null) {
            return CommonResult.build(CommonResult.Code.FAILURE, "缺少博客信息", null);
        } else {
            Example example = new Example(Blog.class);
            example.createCriteria().andEqualTo(Model.ID, id);
            List<Blog> blogs = blogService.selectByExample(example);
            if (null != blogs && blogs.size() == 1) {
                Blog blog = blogs.get(0);
                int readNum = 0;
                if (blog != null) {
                    readNum = blog.getReadNum() + 1;
                    blog.setReadNum(readNum);
                    blogService.updateByPrimaryKeySelective(blog);
                }
                return CommonResult.build(CommonResult.Code.SUCCESS, null, blog);
            } else {
                return CommonResult.build(CommonResult.Code.FAILURE, id + "的blog没有找到或不唯一", null);
            }
        }
    }

    @RequestMapping("/like")
    public CommonResult like(@RequestParam(value = Model.ID) String id) {
        if (id == null) {
            return CommonResult.build(CommonResult.Code.FAILURE, "缺少博客信息", null);
        }
        Example example = new Example(Blog.class);
        example.createCriteria().andEqualTo(Model.ID, id);
        List<Blog> blogs = blogService.selectByExample(example);
        int heartNum = 0;
        if (null != blogs && blogs.size() == 1) {
            Blog blog = blogs.get(0);
            heartNum = blog.getHeartNum() + 1;
            blog.setHeartNum(heartNum);
            blogService.updateByPrimaryKeySelective(blog);
            return CommonResult.build(CommonResult.Code.SUCCESS, null, heartNum);
        } else {
            return CommonResult.build(CommonResult.Code.FAILURE, id + "的blog没有找到或不唯一", null);
        }
    }

    @RequestMapping("/showByQuery")
    public CommonResult showByQuery(@RequestParam(value = KeyConst.PAGE_NUM) Integer pageNum, @RequestParam(value = KeyConst.PAGE_SIZE) Integer pageSize,
            @RequestParam(value = "keyword") String keyword) {
        // 查询数据
        Example example = new Example(Blog.class);
        example.orderBy("createDate").desc();
        PageInfo<Blog> data = null;
        if (keyword == null || keyword.trim().length() == 0) {
            data = blogService.findPagination(example, pageNum, pageSize);
        } else {
            example.createCriteria().orLike("name", "%" + keyword + "%").orLike("content", "%" + keyword + "%");
            data = blogService.findPagination(example, pageNum, pageSize);
        }
        return CommonResult.build(CommonResult.Code.SUCCESS, null, data);
    }

    @RequestMapping("/showByCategory")
    public CommonResult showByCategory(@RequestParam(value = KeyConst.PAGE_NUM) Integer pageNum, @RequestParam(value = KeyConst.PAGE_SIZE) Integer pageSize,
            @RequestParam(value = Model.ID) String categoryID) {
        // 查询数据
        Example example = new Example(Blog.class);
        example.orderBy("createDate").desc();
        PageInfo<Blog> data = null;
        if (categoryID == null || categoryID.trim().length() == 0) {
            data = blogService.findPagination(example, pageNum, pageSize);
        } else {
            example.createCriteria().andEqualTo("categoryID", categoryID);
            data = blogService.findPagination(example, pageNum, pageSize);
        }
        return CommonResult.build(CommonResult.Code.SUCCESS, null, data);
    }

    @RequestMapping("/showByTag")
    public CommonResult showByTag(@RequestParam(value = KeyConst.PAGE_NUM) Integer pageNum, @RequestParam(value = KeyConst.PAGE_SIZE) Integer pageSize, @RequestParam(value = "id") String tagID) {
        // 查询数据
        Tags tags = tagsService.selectByPrimaryKey(tagID);
        if (null != tags) {
            PageInfo<Blog> data = null;
            Example example = new Example(Blog.class);
            example.createCriteria().andLike("tags", "%" + tags.getName() + "%");
            data = blogService.findPagination(example, pageNum, pageSize);
            return CommonResult.build(CommonResult.Code.SUCCESS, null, data);
        } else {
            return CommonResult.build(CommonResult.Code.FAILURE, "没有查询到tags:" + tagID, null);
        }
    }

}
